package layer
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	
	import radevent.RadGameEvent;

	public class BackGroundLayer extends BaseLayer
	{
		private var _bg:Bitmap;
		private var _loader:Loader;
		private var _file:File;
		public function BackGroundLayer()
		{
			super();
			_loader = new Loader();
			initEvents();
		}
		
		private function initEvents():void{
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaderComplete);
		}
		
		private function onLoaderComplete(event:Event):void{
			addChild(_loader);
			dispatchEvent(new RadGameEvent(RadGameEvent.BACKGROUND_CHANGED));
		}
		
		override public function get width():Number
		{
			return _loader.width;
		}
		
		override public function get height():Number
		{
			return _loader.height;
		}
		
		public function set file(f:File):void
		{
			if(f){
				_file = f;
				var fs:FileStream = new FileStream();
				fs.open(f,FileMode.READ);
				var bytes:ByteArray = new ByteArray();
				fs.readBytes(bytes,0,fs.bytesAvailable);
				fs.close();
				_loader.loadBytes(bytes);
			}
		}
		
		override public function getData():*
		{
			var result:Object = {};
			result.url = file.name;
			return result;
		}
		
		public function get file():File
		{
			return _file;
		}
	}
}